<template>
	<view :style="{ height: conentHeight + 'px;' }">
		<view class="top-height">
			<view class="page-top-box">
				<view class="search-box flex ac">
					<u-search borderColor="#DADADA" bgColor="#ffffff" placeholder="搜索项目名称" v-model="searchForm.project_name"
						:showAction="false" @search="searchFunc" @clear="searchFunc"></u-search>
					<view class="search-btn flex ac" @click="searchShow = true" :style="searchShow ? 'color:#3B75FF;' : ''">
						筛选
						<text class="iconfont icon-shaixuan" :style="searchShow ? 'color:#3B75FF;' : ''"></text>
					</view>
				</view>
			</view>
		</view>
		<view class="list-content-box" :style="{ height: 'calc(100% - ' + topHeight + 'px);' }">
			<z-paging ref="paging" :fixed="false" v-model="dataList" @query="queryList"
				:paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }">
				<template slot="empty">
					<u-empty icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" text=" "></u-empty>
				</template>
				<view class="list-items flex ac flex-wrap" :key="index" v-for="(item, index) in dataList"
					@click.stop="navToDetailFunc(item, index)">
					<view class="list-items-title flex ac jb" v-if="$fieldShowFunc('js_xmmc')">
						<view class="title u-line-1">{{ item.project_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('js_xmbh')">
						<view class="title">项目编号</view>
						<view class="desc u-line-1">{{ item.project_number }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('js_xmlx')">
						<view class="title">项目类型</view>
						<view class="desc u-line-1">{{ item.store_type_name || '--' }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('js_cbje')">
						<view class="title">成本金额</view>
						<view class="desc u-line-1">
							{{ item.settlement_cost_amount ? item.settlement_cost_amount + '元' : '--' }}
						</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('js_jsdzt')">
						<view class="title">结算单状态</view>
						<view class="desc u-line-1">
							<template v-if="item.settlement_status == 1">待出具</template>
							<template v-if="item.settlement_status == 2">出具中</template>
							<template v-if="item.settlement_status == 3">已完成</template>
							<template v-if="!item.settlement_status">--</template>
						</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('js_sjwgrq')">
						<view class="title">实际完工日期</view>
						<view class="desc u-line-1">{{ item.actual_completion_date || '--' }}</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('js_cjjsdsj')">
						<view class="title">出具结算单时间</view>
						<view class="desc u-line-1">
							{{ item.settlement_start_date || '--' }}
						</view>
					</view>
					<!-- 按钮 -->
					<view style="width: 100%">
						<judgeBtn plain :per_arr="per_arr" :btns="btns" :state="item.status" :item="item" :idx="index"
							@change="btnsChange"></judgeBtn>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 筛选弹窗 -->
		<u-popup :show="searchShow" round="30" @close="searchShow = false" closeable>
			<view class="search-popup-title">全部筛选</view>
			<scroll-view scroll-y class="search-popup-box">
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>项目类型</text>
						<view class="search-items-title-right flex ac jc" hover-class="hover-class"
							v-if="searchListObj.store_type_id.page != 1" @click="retractFunc('store_type_id')">
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view @click="searchSelectFunc('store_type_id', item)" class="search-items-list-item"
							:class="item.id == searchForm.store_type_id ? 'search-items-list-item-active' : ''"
							hover-class="hover-class" v-for="(item, index) in searchListObj.store_type_id.list" :key="index">
							{{ item.store_type_name }}
						</view>
						<view class="search-items-list-more flex ac jc" hover-class="hover-class"
							@click="lookMoreFunc('store_type_id')"
							v-if="searchListObj.store_type_id.list.length != searchListObj.store_type_id.max">
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>结算单状态</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view @click="searchSelectFunc('settlement_status', item)" class="search-items-list-item"
							:class="item.value == searchForm.settlement_status ? 'search-items-list-item-active' : ''"
							hover-class="hover-class" v-for="(item, index) in searchListObj.settlement_status.list" :key="index">
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">实际完工日期</view>
					<view @click="chooseDateFunc('actual_completion_date')">
						<u--input readonly placeholder="实际完工日期" border="surround"
							:value="searchForm.actual_completion_date_show"></u--input>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">出具结算单时间</view>
					<view @click="chooseDateFunc('settlement_start_date')">
						<u--input readonly placeholder="出具结算单时间" border="surround"
							:value="searchForm.settlement_start_date_show"></u--input>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">结算完成时间</view>
					<view @click="chooseDateFunc('settlement_completion_date')">
						<u--input readonly placeholder="结算完成时间" border="surround"
							:value="searchForm.settlement_completion_date_show"></u--input>
					</view>
				</view>
			</scroll-view>
			<view class="btn-box flex ac jb">
				<u-button @click="reSearchValFunc" text="重置"
					customStyle="width: 248rpx;height: 82rpx;background: #242834;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"></u-button>
				<u-button @click="searchFunc" text="查询"
					customStyle="width: 422rpx;height: 82rpx;background: #3B75FF;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"></u-button>
			</view>
		</u-popup>
		<!-- 创建时间筛选 -->
		<u-calendar :show="dateShow" mode="range" @confirm="dateConfirmFunc" @close="dateShow = false" :maxDate="maxDate"
			:minDate="minDate" :defaultDate="defaultDate" :monthNum="36"></u-calendar>
	</view>
</template>

<script>
	export default {
		name: '',
		props: {
			conentHeight: {
				default: 0
			},
			option: {
				type: Object,
				default: {}
			}
		},
		data() {
			const d = new Date();
			const year = d.getFullYear();
			let month = d.getMonth() + 1;
			month = month < 10 ? `0${month}` : month;
			const date = d.getDate();
			return {
				per_arr: [],
				btns: [{
						per_name: 'settlemen_examine',
						state: 1135,
						name: '结算单初审'
					},
					{
						per_name: 'preSettlement_reexamine',
						state: 1138,
						name: '结算单复审'
					}
				],
				oper_item: {},
				per_name: '',
				oper_idx: '',

				defaultDate: [`${year}-${month}-${date}`],
				maxDate: `${year}-${month}-${date}`, //日历最大日期
				minDate: `${year - 2}-${month}-${date}`, //日历最小日期
				dateShow: false,
				searchShow: false,
				dataList: [],
				searchForm: {
					project_name: '',
					store_type_id: '',
					settlement_status: '',
					dateTimeType: '',
					actual_completion_date_start: '',
					actual_completion_date_end: '',
					actual_completion_date_show: '',
					settlement_start_date_start: '',
					settlement_start_date_end: '',
					settlement_start_date_show: '',
					settlement_completion_date_start: '',
					settlement_completion_date_end: '',
					settlement_completion_date_show: ''
				},
				topHeight: 0,
				searchListObj: {
					store_type_id: {
						list: [],
						page: 1,
						num: 5,
						max: 0
					},
					settlement_status: {
						list: [{
								label: '待出具',
								value: 1
							},
							{
								label: '出具中',
								value: 2
							},
							{
								label: '已完成',
								value: 3
							}
						],
						page: 1,
						num: 5,
						max: 0
					}
				},

			};
		},
		mounted() {
			this.getHeight();
			this.getStoreTypeList();

			// 获取权限
			this.$Api.getHtmlAuthority({
				path: '/settlementManagement'
			}).then((res) => {
				console.log(res, 'getHtmlAuthority');
				if (res.code == 200) {
					this.per_arr = res.data.permission;
				}
			});
		},
		watch: {},
		methods: {
			// 重置弹窗筛选项
			reSearchValFunc() {
				this.searchForm = {
					project_name: '',
					store_type_id: '',
					settlement_status: '',
					dateTimeType: '',
					actual_completion_date_start: '',
					actual_completion_date_end: '',
					actual_completion_date_show: '',
					settlement_start_date_start: '',
					settlement_start_date_end: '',
					settlement_start_date_show: '',
					settlement_completion_date_start: '',
					settlement_completion_date_end: '',
					settlement_completion_date_show: ''
				};
				this.$nextTick(() => {
					this.searchFunc();
				});
			},
			// 刷新列表
			searchFunc() {
				this.$refs.paging.reload();
				this.searchShow = false;
			},
			getHeight() {
				// 创建节点查询
				let that = this;
				const systemInfo = uni.getSystemInfoSync();
				const query = uni.createSelectorQuery().in(that);
				query.select('.top-height').boundingClientRect();
				query.exec((res) => {
					that.topHeight = res[0].height + systemInfo.statusBarHeight + 20;
				});
			},
			// 去详情
			navToDetailFunc(item, index) {
				this.oper_idx = index;
				uni.navigateTo({
					url: '/pagesB/settlementManagementDetail/settlementManagementDetail?id=' + item.id
				});
			},
			queryList(page, num) {
				this.getSettlementList(page, num);
			},
			// 结算列表
			getSettlementList(page, num) {
				let searchForm = this.searchForm;
				uni.showLoading({
					mask: true
				});
				this.$Apipc
					.getSettlementList({
						page,
						num,
						project_name: searchForm.project_name,
						store_type_id: searchForm.store_type_id,
						settlement_status: searchForm.settlement_status,
						actual_completion_date_start: searchForm.actual_completion_date_start,
						actual_completion_date_end: searchForm.actual_completion_date_end,
						settlement_start_date_start: searchForm.settlement_start_date_start,
						settlement_start_date_end: searchForm.settlement_start_date_end,
						settlement_completion_date_start: searchForm.settlement_completion_date_start,
						settlement_completion_date_end: searchForm.settlement_completion_date_end
					})
					.then((res) => {
						uni.hideLoading();
						console.log(res, '结算列表');
						if (res.code == 200) {
							this.$refs.paging.complete(res.data.list);
						}
					});
			},
			// 弹窗筛选项
			searchSelectFunc(type, item) {
				console.log(type, item);
				switch (type) {
					case 'store_type_id':
						this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
						break;
					case 'settlement_status':
						this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
						break;
					default:
				}
			},
			// 弹窗筛选项查看更多
			lookMoreFunc(type) {
				switch (type) {
					case 'store_type_id':
						this.searchListObj.store_type_id.page += 1;
						this.getStoreTypeList();
						break;
					default:
				}
			},
			// 弹窗筛选项列表收起
			retractFunc(type) {
				switch (type) {
					case 'store_type_id':
						this.searchListObj.store_type_id.page = 1;
						this.getStoreTypeList();
						break;
					default:
				}
			},
			// 门店类型列表(项目类型)
			getStoreTypeList() {
				this.$Apipc
					.getStoreTypeList({
						page: this.searchListObj.store_type_id.page,
						num: this.searchListObj.store_type_id.num
					})
					.then((res) => {
						console.log(res, '门店类型列表');
						if (res.code == 200) {
							if (this.searchListObj.store_type_id.page == 1) {
								this.searchListObj.store_type_id.list = [];
							}
							this.searchListObj.store_type_id.list = [...this.searchListObj.store_type_id.list, ...res.data.list];
							this.searchListObj.store_type_id.max = res.data.max;
						}
					});
			},
			// 创建时间日历弹窗
			chooseDateFunc(type) {
				this.searchForm.dateTimeType = type;
				this.dateShow = true;
			},
			// 日期筛选确认
			dateConfirmFunc(e) {
				switch (this.searchForm.dateTimeType) {
					case 'actual_completion_date':
						this.searchForm.actual_completion_date_start = e[0];
						this.searchForm.actual_completion_date_end = e[e.length - 1];
						this.searchForm.actual_completion_date_show = e[0] + '~' + e[e.length - 1];
						break;
					case 'settlement_start_date':
						this.searchForm.settlement_start_date_start = e[0];
						this.searchForm.settlement_start_date_end = e[e.length - 1];
						this.searchForm.settlement_start_date_show = e[0] + '~' + e[e.length - 1];
						break;
					case 'settlement_completion_date':
						this.searchForm.settlement_completion_date_start = e[0];
						this.searchForm.settlement_completion_date_end = e[e.length - 1];
						this.searchForm.settlement_completion_date_show = e[0] + '~' + e[e.length - 1];
						break;
					default:
				}
				this.dateShow = false;
			},

			// 操作按钮触发点击
			btnsChange(e) {
				console.log(e, 'xxxsww');
				this.oper_item = e.item;
				this.oper_idx = e.index;
				uni.navigateTo({
					url: '/pagesB/settlementManagementDetail/settlementManagementDetail?id=' + e.item.id
				});
			},
			updateListIdx() {
				console.log('来了 老弟');
				let idx = this.oper_idx.toString();
				if (idx) {
					let item = this.dataList[idx]
					this.$Apipc.getSettlementDetails({
						project_id: item.id
					}).then((res) => {
						console.log(res, '预算管理详情');
						if (res.code == 200) {
							let info = res.data
							this.dataList[idx] = res.data
							this.dataList.splice(0, 0)
						}
					});
				}
			},
		}
	};
</script>

<style lang="scss"></style>